package com.rigi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rigi.common.R;
import com.rigi.entity.Employee;
import com.rigi.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username", employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);
        if (emp == null) {
            return R.error("该用户名不存在");
        }
        if (!password.equals(emp.getPassword())) {
            return R.error("密码错误");
        }
        if (emp.getStatus() == 0) {
            return R.error("该用户已被禁用");
        }
        session.setAttribute("employee", emp);
        return R.success(emp);
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize,String name) {
        Page<Employee> employeePage = new Page<>(page, pageSize);
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        if (name!=null){
            wrapper.like("name",name);
        }
        wrapper.orderByDesc("create_time");
        Page<Employee> page1 = employeeService.page(employeePage, wrapper);
        return R.success(page1);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee) {
        String password=DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
        boolean save = employeeService.save(employee);
        if (save) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> edit(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        boolean update = employeeService.updateById(employee);
        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
